package com.jjtm.common.core.expections;


import com.jjtm.common.core.constants.ErrorConstants;
import com.jjtm.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * 
 * @author wangbaofeng
 *
 */
@ControllerAdvice
@Slf4j
@Order(-100)
public class GlobalExceptionHandler {


	@ResponseBody
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public JsonResult httpRequestMethodNotSupportedException(HttpServletResponse response, HttpRequestMethodNotSupportedException ex) {
		JsonResult result = new JsonResult(ErrorConstants.BUG_ERR, false, "非法请求！");
		ex.printStackTrace();
		return result;
	}
	
	
	@ResponseBody
	@ExceptionHandler(Exception.class)
	public JsonResult exception(HttpServletResponse response, Exception ex) {
		JsonResult result = new JsonResult(ErrorConstants.BUG_ERR, false, "网络异常！");
		ex.printStackTrace();
		return result;
	}

	@ResponseBody
	@ExceptionHandler(NullPointerException.class)
	public JsonResult nullPointer(HttpServletResponse response, NullPointerException ex) {
		log.error("空指针异常");
		JsonResult result = new JsonResult(ErrorConstants.BUG_ERR, false, "系统异常！");
		ex.printStackTrace();
		return result;
	}

	@ResponseBody
	@ExceptionHandler(RuntimeException.class)
	public JsonResult runTime(HttpServletResponse response, RuntimeException ex) {
		JsonResult result = new JsonResult(ErrorConstants.BUG_ERR, false, "操作异常！");
		ex.printStackTrace();
		return result;
	}
	
	@ResponseBody
	@ExceptionHandler(DuplicateKeyException.class)
	public JsonResult duplicateKey(HttpServletResponse response, DuplicateKeyException ex) {
		JsonResult result = new JsonResult(ErrorConstants.BUG_ERR, false, "数据重复！");
		return result;
	}
	
	

	@ResponseBody
	@ExceptionHandler(IndexOutOfBoundsException.class)
	public JsonResult indexOutOfBounds(HttpServletResponse response, IndexOutOfBoundsException ex) {
		JsonResult result = new JsonResult(ErrorConstants.BUG_ERR, false, "非法操作！");
		ex.printStackTrace();
		return result;
	}

	@ResponseBody
	@ExceptionHandler(BussinessException.class)
	public JsonResult bussinessException(HttpServletResponse response, BussinessException ex) {
		log.error(ex.getMessage());
		JsonResult result = new JsonResult(ex.getCode(), false, ex.getMessage());
		return result;
	}

	@ResponseBody
	@ExceptionHandler(BindException.class)
	public JsonResult bindException(HttpServletResponse response, BindException ex) {
		log.error(ex.getMessage());
		JsonResult result = new JsonResult(ErrorConstants.BUG_ERR, false, Objects.requireNonNull(ex.getBindingResult().getFieldError()).getDefaultMessage());
		return result;
	}
}
